/*
 * @Author: jiangjunfeng
 * @Date: 2021-08-20 15:58:21
 * @LastEditTime: 2021-11-18 16:53:19
 * @LastEditors: 江俊锋
 * @Description: In User Settings Edit
 * @FilePath: \sass-web\packages\empower-web\src\views\setting\views\print\utils\validator.js
 */

import {message} from 'ant-design-vue'

function validate() {
    if (!this.rules) this.rules = [];
    for (var i = 0; i < this.rules.length; i++) {
        var item = this.rules[i], keyArray, valueType, value;
        if (!item.prop) throw new Error('请填写prop');
        keyArray = item.prop.split('.'), value = this.context[keyArray[0]];
        for (var a = 1; a < keyArray.length; a++) {
            value = value[keyArray[a]];
        }
        valueType = Object.prototype.toString.call(value).slice(8, -1);
        if (item.validator) {
            
            try {
               item.validator.call(this.context, value)
            } catch (e) {
                console.error(e);
                this.failCallback(e.message)
                throw new Error(item.prop+'验证未通过');
            }
        } else {
            if (!value && item.require) {
                this.failCallback(item.msg);
                throw new Error(item.prop+'验证未通过');
            }
            switch (valueType) {
                case 'String':
                    if (item.require) {
                        if (!value) {
                            this.failCallback(item.msg);
                            throw new Error(item.prop+'验证未通过');
                        }
                    }
                    if (item.maxLength) {
                        if (value.length > item.maxLength) {
                            this.failCallback(item.msg);
                            throw new Error(item.prop+'验证未通过');
                        }
                    }
                    if (item.minLength) {
                        if (value.length < item.minLength) {
                            this.failCallback(item.msg);
                            throw new Error(item.prop+'验证未通过');
                        }
                    }
                    break;
                case 'Array':
                    if (item.maxLength) {
                        if (value.length > item.maxLength) {
                            this.failCallback(item.msg);
                            throw new Error(item.prop+'验证未通过');
                        }
                    }
                    if (item.minLength) {
                        if (value.length < item.minLength) {
                            this.failCallback(item.msg);
                            throw new Error(item.prop+'验证未通过');
                        }
                    }
                    break;
                case 'Number':
                    if (item.max != null || item.max !== undefined) {
                        if (value > item.max) {
                            this.failCallback(item.msg);
                            throw new Error(item.prop+'验证未通过');
                        }
                    }
                    if (item.min != null || item.min !== undefined) {
                        if (value < item.min) {
                            this.failCallback(item.msg);
                            throw new Error(item.prop+'验证未通过');
                        }
                    }
                    break;
            }
        }
    }
    return true;
}

function defaultErrorCallBack(msg) {
    if (message && message.error) message.error(msg);
    else throw new Error(msg);
}

export default function Validator(context, rules) {
    this.context = context
    this.rules = rules
}
// 定义的验证函数的this会指向传入的初始化时的对象，可以方便取值
Validator.prototype.validate = validate
Validator.prototype.failCallback = defaultErrorCallBack
Validator.prototype.setFailCallback = function(cb) {
    this.failCallback = cb
}